package main

import (
	"fmt"
	"github.com/go-redis/redis"
	"log"
	"strings"
)

type MessageSub struct {
	Pattern *string
	Channel *string
	Payload *string
}

func main() {
	rdb := redis.NewClient(&redis.Options{
		Addr:     "43.255.53.66:6679",
		Password: "zack@root123.com",
		DB:       0,
	})

	pong, err := rdb.Ping().Result() // 检查是否连接
	if err != nil {
		log.Fatal("连接失败：", err)
	}

	// 连接成功啦
	fmt.Println("连接成功：", pong)

	// 订阅全部消息
	pubsub := rdb.PSubscribe("__keyevent@0__:expired")
	// 等待消息返回，原因是上一个方法不是立即返回的，囧
	_, err = pubsub.Receive()
	if err != nil {
		log.Fatal(err)
	}

	// 处理消息
	for msg := range pubsub.Channel() {
		var orderIdKeys []string
		key := msg.Payload
		fmt.Println("key:", key)
		fmt.Println(strings.Contains(msg.Payload, "LIVE:SUPPORT:EXPIRE:"))
		if strings.Contains(msg.Payload, "LIVE:SUPPORT:EXPIRE:") {
			orderIdKeys = append(orderIdKeys, strings.Replace(msg.Payload, "LIVE:SUPPORT:EXPIRE:", "", 1))
		}
		fmt.Println("orderIdKeys1:", orderIdKeys)
	}

	//msgSub := new(MessageSub)
	//for msg := range pubsub.Channel() {
	//	switch msg.(type) {
	//	case redis.Message:
	//		msgSub.Pattern = (*string)(unsafe.Pointer(&msg.Pattern))
	//		msgSub.Channel = (*string)(unsafe.Pointer(&msg.Channel))
	//		msgSub.Payload = (*string)(unsafe.Pointer(&msg.Payload))
	//	default:
	//		continue
	//	}
	//}

}
